﻿using System;
using System.Collections.Generic;
using System.Linq;
using Altairis.FluffyCloud.Data;

namespace Altairis.FluffyCloud.WebCore.Pages.My {

    public partial class ProfileAcl : System.Web.UI.Page {

        public void AclEntriesList_DeleteItem(Guid aclEntryId) {
            using (var dc = new FluffyDbContext()) {
                var entry = dc.UserAclEntries.FirstOrDefault(x => x.UserName.Equals(this.User.Identity.Name, StringComparison.CurrentCultureIgnoreCase) && x.AclEntryId == aclEntryId);
                if (entry == null) return;
                dc.UserAclEntries.Remove(entry);
                dc.SaveChanges();
                this.Response.Redirect(this.Request.Url.ToString());
            }
        }

        public IEnumerable<UserAclEntry> AclEntriesList_GetData() {
            using (var dc = new FluffyDbContext()) {
                var q = from e in dc.UserAclEntries.Include("Role")
                        where e.UserName.Equals(this.User.Identity.Name, StringComparison.CurrentCultureIgnoreCase)
                        orderby e.Operation, e.Action
                        select e;
                return q.ToArray();
            }
        }

        public void AclEntriesList_InsertItem(UserAclEntry item) {
            item.UserName = this.User.Identity.Name;
            using (var dc = new FluffyDbContext()) {
                dc.UserAclEntries.Add(item);
                dc.SaveChanges();
                this.Response.Redirect(this.Request.Url.ToString());
            }
        }

        public IEnumerable<object> RoleList_GetData() {
            using (var dc = new FluffyDbContext()) {
                var q = from r in dc.Roles
                        orderby r.RoleName
                        select new {
                            Text = r.RoleName + " (" + r.Description + ")",
                            Value = r.RoleName
                        };
                return q.ToArray();
            }
        }

        protected void Page_Load(object sender, EventArgs e) {
        }

        protected void ResetButton_Click(object sender, EventArgs e) {
            using (var dc = new FluffyDbContext()) {
                var entriesToDelete = dc.UserAclEntries.Where(x => x.UserName.Equals(this.User.Identity.Name, StringComparison.CurrentCultureIgnoreCase)).ToList();
                foreach (var entry in entriesToDelete) {
                    dc.UserAclEntries.Remove(entry);
                }
                dc.UserAclEntries.Add(new UserAclEntry { UserName = this.User.Identity.Name, RoleName = Role.EveryoneRoleName, Operation = UserAclEntry.OperationReadProfileContacts, Action = true });
                dc.UserAclEntries.Add(new UserAclEntry { UserName = this.User.Identity.Name, RoleName = Role.EveryoneRoleName, Operation = UserAclEntry.OperationReadProfilePersonal, Action = true });
                dc.UserAclEntries.Add(new UserAclEntry { UserName = this.User.Identity.Name, RoleName = Role.EveryoneRoleName, Operation = UserAclEntry.OperationSendPrivateMessage, Action = true });
                dc.SaveChanges();
                this.Response.Redirect(this.Request.Url.ToString());
            }
        }
    }
}